package com.autoMobile.dao;

import com.autoMobile.bean.Vehicle;
import com.autoMobile.bean.auto_management;
import com.autoMobile.bean.ddmUser1;
import com.autoMobile.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class auto_managementDao {

    // 查询所有车辆信息
    public static List<auto_management> getAutoManagement() {
        Connection conn = null;
        PreparedStatement psts = null;
        ResultSet rs = null;
        List<auto_management> auto_managements = new ArrayList<auto_management>();
        String sql = "SELECT * FROM auto_management";
        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            rs = psts.executeQuery();
            while (rs.next()) {
                if (rs.getString(14).equals("申请")  || rs.getString(14).equals("还车登记")){
                    auto_management auto_management = new auto_management(rs.getInt(1),rs.getString(2), rs.getString(3),
                            rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8),
                            rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13),
                            rs.getString(14));
                    auto_managements.add(auto_management);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, rs);
        }
        return auto_managements;
    }

    public static int updateFieldsLiCheng(ddmUser1 ddm) {
        Connection conn = null;
        PreparedStatement psts = null;
        int rowsAffected = 0;
        String sql = "UPDATE auto_management SET soil = ?, omil = ?, zhuang = ?, shuo = ?, okm = ?, ooil = ?, ram = ? WHERE message = ?";

        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            // Setting parameters for update
            psts.setString(1, ddm.getSoil());
            psts.setString(2, ddm.getOmil());
            psts.setString(3, ddm.getZhuang());
            psts.setString(4, ddm.getShuo());
            psts.setString(5, ddm.getOkm());
            psts.setString(6, ddm.getOoil());
            psts.setString(7, ddm.getRam());
            psts.setString(8, ddm.getMessage()); // The ID of the record to update

            rowsAffected = psts.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, null); // Close resources
        }
        return rowsAffected; // If at least one row was affected, update was successful
    }

    public static int upSenQinServlet(String message,String zt) {
        Connection conn = null;
        PreparedStatement psts = null;
        int rowsAffected = 0;
        String sql = "UPDATE auto_management SET zhuang = ? WHERE message = ?";

        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            // Setting parameters for update
            psts.setString(1, zt);
            psts.setString(2, message); // The ID of the record to update

            rowsAffected = psts.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, null); // Close resources
        }
        return rowsAffected; // If at least one row was affected, update was successful
    }

    public static int deleteAutoManagement(String message) {
        Connection conn = null;
        PreparedStatement psts = null;
        int affectedRows = 0;
        String sql = "DELETE FROM auto_management WHERE message=?";
        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            psts.setString(1, message);
            affectedRows = psts.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, null);
        }
        return affectedRows;
    }
}